package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.AddPromotionProductBean;
import com.ymatou.iapi.thirdpartysync.service.AddPromotionProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/**
 * 添加推广商品Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddPromotionProduct {
	private static AddPromotionProductBean addpromotionproductBean;
	private static AddPromotionProductCall addpromotionproductCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加推广商品Test");
	}

	@Before
	public void caseUp() throws Exception {
		addpromotionproductBean = new AddPromotionProductBean();
		addpromotionproductCall = new AddPromotionProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addpromotionproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AddPromotionProduct_001() {
		Logger.start(true, "正确数据，成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Thread.sleep(3000);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
//			// 创建一个活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, 207104,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Thread.sleep(4000);
			addpromotionproductBean.setProductId(productId);
			addpromotionproductBean.setPromoterCommissions(null);
			addpromotionproductBean.setCommission(0.03);
			addpromotionproductBean.setStartTime(YMTDateUtil.getBeforeOrNextDay(-1, YMTDateUtil.YMDHMS));
			addpromotionproductBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(5, YMTDateUtil.YMDHMS));
			addpromotionproductBean.setCategoryIds("1,2,3");
			addpromotionproductBean.setCategoryNames("a,b,c");
			addpromotionproductCall.setData(addpromotionproductBean);
			addpromotionproductCall.callService();
			Logger.verifyEquals("200", addpromotionproductCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}